package com.longma.server.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.longma.server.constant.enums.ResponseCode;
import com.longma.server.dao.InspectionProjectDAO;
import com.longma.server.entity.InspectionProject;
import com.longma.server.exception.BusinessException;
import com.longma.server.pojo.dto.InspectionProjectDTO;
import com.longma.server.pojo.dto.InspectionProjectUpdateDTO;
import com.longma.server.service.IInspectionProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@Service
public class InspectionProjectServiceImpl implements IInspectionProjectService {
    @Autowired
    InspectionProjectDAO inspectionProjectDAO;

    /**
     * 新增巡視項目
     */
    @Override
    public InspectionProject addInspectionProject(InspectionProjectDTO addRequestDTO) {
        InspectionProject inspectionProject = new InspectionProject();
        BeanUtil.copyProperties(addRequestDTO,inspectionProject);
        inspectionProjectDAO.save(inspectionProject);
        return inspectionProject;
    }

    /**
     * 根据巡視項目id删除巡視項目
     */
    @Override
    public boolean deleteInspectionProjectById(Integer id) {
        if (!isExistId(id)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }
        return inspectionProjectDAO.removeById(id);
    }

    /**
     * 根据巡視項目id列表删除巡視項目
     */
    @Override
    public boolean deleteInspectionProjectByIds(List<Integer> ids) {
        if (!isExistIds(ids)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        return inspectionProjectDAO.removeByIds(ids);
    }
    /**
     * 根据巡視記錄id查找巡視項目
     */
    @Override
    public List<InspectionProject> getInspectionProjectByRecordId(Integer rid) {
        return inspectionProjectDAO.list(Wrappers.<InspectionProject>lambdaQuery().eq(InspectionProject::getInspectionRecordId,rid));
    }

    /**
     * 更新巡視項目
     */
    @Override
    public InspectionProject updateInspectionProject(InspectionProjectUpdateDTO updateRequestDTO) {
        // 存在校验
        if(inspectionProjectDAO.getById(updateRequestDTO.getId()) == null){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        // 复制
        InspectionProject inspectionProject = new InspectionProject();
        BeanUtil.copyProperties(updateRequestDTO,inspectionProject);
        inspectionProjectDAO.updateById(inspectionProject);
        return inspectionProject;
    }

    /**
     * 判断id列表是否存在
     */
    private boolean isExistIds(List<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return false;
        }
        LambdaQueryWrapper<InspectionProject> wrapper = Wrappers.lambdaQuery();
        Set<Integer> productIds = new HashSet<>(ids);
        return inspectionProjectDAO.count(wrapper.in(InspectionProject::getId, productIds)) == productIds.size();
    }

    /**
     * 判断id是否存在
     */
    private boolean isExistId(Integer id) {
        return isExistIds(Arrays.asList(id));
    }
}
